/*
 * Copyright [2022] [https://www.xiaonuo.vip]
 *
 * Snowy采用APACHE LICENSE 2.0开源协议，您在使用过程中，需要注意以下几点：
 *
 * 1.请不要删除和修改根目录下的LICENSE文件。
 * 2.请不要删除和修改Snowy源码头部的版权声明。
 * 3.本项目代码可免费商业使用，商业使用请保留源码和相关描述文件的项目出处，作者声明等。
 * 4.分发源码时候，请注明软件出处 https://www.xiaonuo.vip
 * 5.不可二次分发开源参与同类竞品，如有想法可联系团队xiaonuobase@qq.com商议合作。
 * 6.若您的项目无法满足以上几点，需要更多功能代码，获取Snowy商业授权许可，请在官网购买授权，地址为 https://www.xiaonuo.vip
 */
package vip.xiaonuo.biz.modular.deviceapitool;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import vip.xiaonuo.common.pojo.CommonResult;
import vip.xiaonuo.common.util.SignTool;
import vip.xiaonuo.dev.api.DevConfigApi;

@Api(tags = "模拟设备端接口请求")
@ApiSupport(author = "SNOWY_TEAM", order = 1)
@RestController
@Validated
@Slf4j
public class BizDeviceApiToolController {
    public static final String base_url = "http://127.0.0.1:18823";
    @Autowired
    private DevConfigApi devConfigApi;

    /**
     * @author kongxm
     * @date 2022/11/20 17:04
     */
    @PostMapping("/biz/deviceapitool/opendoor")
    public CommonResult<JSONObject> openDoor(@RequestBody String reqJson) {
        String token = devConfigApi.getValueByKey("GLOBAL_DEVICE_TOKEN");
        JSONObject jsonObject = JSONObject.parseObject(reqJson, Feature.OrderedField);
        if (StringUtils.isEmpty(token)) return CommonResult.error("柜子没有配置signToken");
        String url = base_url + "/api/client/opendoor";
        log.info("url={}", url);
        String resp = "";
        try {
            resp = HttpUtil.post(url, handleReq(reqJson, token));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return CommonResult.data(JSONObject.parseObject(resp));
    }

    /**
     * @author kongxm
     * @date 2022/11/20 17:04
     */
    @PostMapping("/biz/deviceapitool/closedoor")
    public CommonResult<JSONObject> closeDoor(@RequestBody String reqJson) {
        String token = devConfigApi.getValueByKey("GLOBAL_DEVICE_TOKEN");
        JSONObject jsonObject = JSONObject.parseObject(reqJson, Feature.OrderedField);
        if (StringUtils.isEmpty(token)) return CommonResult.error("柜子没有配置signToken");
        String url = base_url + "/api/client/closedoor";
        log.info("url={}", url);
        String resp = "";
        try {
            resp = HttpUtil.post(url, handleReq(reqJson, token));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return CommonResult.data(JSONObject.parseObject(resp));
    }

    /**
     * @author kongxm
     * @date 2022/11/20 17:04
     */
    @PostMapping("/biz/deviceapitool/inventoy")
    public CommonResult<JSONObject> inventoy(@RequestBody String reqJson) {
        String token = devConfigApi.getValueByKey("GLOBAL_DEVICE_TOKEN");
        JSONObject jsonObject = JSONObject.parseObject(reqJson, Feature.OrderedField);
        JSONArray jsonArray = jsonObject.getJSONArray("data");
        if (StringUtils.isEmpty(token)) return CommonResult.error("柜子没有配置signToken");
        String url = base_url + "/api/client/inventoy";
        log.info("url={}", url);
        String resp = "";
        try {
            resp = HttpUtil.post(url, handleReq(reqJson, token));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return CommonResult.data(JSONObject.parseObject(resp));
    }


    private String handleReq(String reqJson, String token) {
        String sign = SignTool.signMd5Hex16(reqJson, token);
        JSONObject jsonObject = JSONObject.parseObject(reqJson, Feature.OrderedField);
        jsonObject.put("sign", sign);
        log.info(jsonObject.toJSONString());
        return jsonObject.toJSONString();
    }
}
